package com.bjpowernode.crm.workbench.utils;

import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.workbench.domain.Activity;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

public class DownLoadUtils {
    public static void downloadActivity(List<Activity> activityList, HttpServletResponse response) throws IOException {
        /*String path = request.getSession().getServletContext().getRealPath("/WEB-INF/classes") + "/result.xls";*/

        //创建Excel表格
        //创建一个Excel
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建页
        HSSFSheet sheet = workbook.createSheet("活动列表");

        HSSFRow title = sheet.createRow(0);
        title.createCell(0).setCellValue("id");
        title.createCell(1).setCellValue("owner");
        title.createCell(2).setCellValue("name");
        title.createCell(3).setCellValue("startDate");
        title.createCell(4).setCellValue("endDate");
        title.createCell(5).setCellValue("cost");
        title.createCell(6).setCellValue("description");
        title.createCell(7).setCellValue("creatTime");
        title.createCell(8).setCellValue("creatBy");
        title.createCell(9).setCellValue("editTime");
        title.createCell(10).setCellValue("editBy");

        for(int i = 0;i < activityList.size();i++){
            HSSFRow row = sheet.createRow(i+1);
            row.createCell(0).setCellValue(activityList.get(i).getId());
            row.createCell(1).setCellValue(activityList.get(i).getOwner());
            row.createCell(2).setCellValue(activityList.get(i).getName());
            row.createCell(3).setCellValue(activityList.get(i).getStartDate());
            row.createCell(4).setCellValue(activityList.get(i).getEndDate());
            row.createCell(5).setCellValue(activityList.get(i).getCost());
            row.createCell(6).setCellValue(activityList.get(i).getDescription());
            row.createCell(7).setCellValue(activityList.get(i).getCreateTime());
            row.createCell(8).setCellValue(activityList.get(i).getCreateBy());
            row.createCell(9).setCellValue(activityList.get(i).getEditTime());
            row.createCell(10).setCellValue(activityList.get(i).getEditBy());
        }

        String fileName = DateUtils.formatDataTime(new Date());
        response.setContentType("application/octet-stream;charset=UTF-8");
        response.addHeader("Content-Disposition","attachment;filename="+fileName+".xls");
        workbook.write(response.getOutputStream());

        /*File file = new File(path);
        try {
            workbook.write(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        FileInputStream fis = null;
        try {
            OutputStream out = response.getOutputStream();
            fis = new FileInputStream(path);

            byte[] buff = new byte[256];

            int res = -1;
            while((res=fis.read(buff))!=-1){
                out.write(buff,0,res);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }*/
    }
}
